/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.PassPortConst;
import com.apache.passport.entity.Token;

/**
 * description:  校验外部系统访问的token是否已失效
 *
 * @author Hou Dayu 创建时间：2016-12-29
 */
public class CheckTokenIdPlugin implements PluginConnector {

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see PluginConnector#execute(ParamsVo)
     */
    @Override
    public Object execute(ParamsVo paramsvo) throws Exception {
        ResultEntity result = new ResultEntity();
        result.setResult("true");
        String tokenId = (String) paramsvo.getParams("tokenId");
        Token token = PassPortConst.getToken(tokenId);
        if (Validator.isEmpty(token)) {
            result.setEntity("F");
            result.setMessage("认证已过期或未登录");
            return result;
        }
        String loginType = String.valueOf(paramsvo.getParams("loginType"));
        if (Validator.isNull(loginType)) {
            result.setEntity(tokenId);
        } else {
            result.setEntity(token);
        }
        result.setMessage(DesUtils.getInstance().decrypt(token.getUserEname()));
        return result;
    }

}
